#include "volttestresult.h"
#include "ui_volttestresult.h"
#include <QMessageBox>
#include "msgbox.h"

VoltTestResult::VoltTestResult(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VoltTestResult)
{
    ui->setupUi(this);
    bdf = BllDataFlow::getInstance();

    //Signals and Slots
    connect(bdf, &BllDataFlow::updateVoltTestResultSignal,this, &VoltTestResult::updateVoltTestResultSlot);
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &VoltTestResult::retranslateUiSlot);

    initWidget();
}

VoltTestResult::~VoltTestResult()
{
    delete ui;
}

void VoltTestResult::show(const VoltTest &vt)
{
    updateVoltTestResultSlot(vt);
    QWidget::show();
}

void VoltTestResult::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(255, 255, 255, 0));
}

void VoltTestResult::on_btnExportVoltRes_clicked()
{
    //todo:export volt test result
    //todo:log?
    MsgBox::information(this,QString(tr("")),QString(tr("Volt test result exported ok.")));
}

void VoltTestResult::on_btnOK_clicked()
{
    this->hide();
}

void VoltTestResult::on_btnClose_clicked()
{
    this->hide();
}

void VoltTestResult::updateVoltTestResultSlot(const VoltTest &vt)
{
    QString paramStr = QString("<span style=\"color:#000;\">%1V</span>/<span style=\"color:#000;\">%2μs</span>");
    QString offsetStr = QString("<span style=\"color:%1;\">%2%</span>/<span style=\"color:%3;\">%4%</span>");
    QLabel * lowSetLabel = new QLabel();
    QLabel * lowGetLabel = new QLabel();
    QLabel * lowOffsetLabel = new QLabel();
    QLabel * mediumSetLabel = new QLabel();
    QLabel * mediumGetLabel = new QLabel();
    QLabel * mediumOffsetLabel = new QLabel();
    QLabel * highSetLabel = new QLabel();
    QLabel * highGetLabel = new QLabel();
    QLabel * highOffsetLabel = new QLabel();

    lowSetLabel->setText(paramStr.arg(doubleToString(vt.lowVolt.setValue, 0), doubleToString(vt.lowPulseWidth.setValue, 0)));
    lowGetLabel->setText(paramStr.arg(doubleToString(vt.lowVolt.realValue, 1), doubleToString(vt.lowPulseWidth.realValue, 0)));
    lowOffsetLabel->setText(offsetStr.arg("#000", doubleToString((vt.lowVolt.realValue - vt.lowVolt.setValue) / vt.lowVolt.setValue * 100, 1),
                                          "#000", doubleToString((vt.lowPulseWidth.realValue - vt.lowPulseWidth.setValue) / vt.lowPulseWidth.setValue * 100, 1)));

    mediumSetLabel->setText(paramStr.arg(doubleToString(vt.mediumVolt.setValue, 0), doubleToString(vt.mediumPulseWidth.setValue, 0)));
    mediumGetLabel->setText(paramStr.arg(doubleToString(vt.mediumVolt.realValue, 1), doubleToString(vt.mediumPulseWidth.realValue, 0)));
    mediumOffsetLabel->setText(offsetStr.arg("#000", doubleToString((vt.mediumVolt.realValue - vt.mediumVolt.setValue) / vt.mediumVolt.setValue * 100, 1),
                                            "#000", doubleToString((vt.mediumPulseWidth.realValue - vt.mediumPulseWidth.setValue) / vt.mediumPulseWidth.setValue * 100, 1)));

    highSetLabel->setText(paramStr.arg(doubleToString(vt.highVolt.setValue, 0), doubleToString(vt.highPulseWidth.setValue, 0)));
    highGetLabel->setText(paramStr.arg(doubleToString(vt.highVolt.realValue, 1), doubleToString(vt.highPulseWidth.realValue, 1)));
    highOffsetLabel->setText(offsetStr.arg("#000", doubleToString((vt.highVolt.realValue - vt.highVolt.setValue) / vt.highVolt.setValue * 100, 1),
                                           "#000", doubleToString((vt.highPulseWidth.realValue - vt.highPulseWidth.setValue) / vt.highPulseWidth.setValue * 100, 1)));

    lowSetLabel->setAlignment(Qt::AlignCenter);
    lowGetLabel->setAlignment(Qt::AlignCenter);
    lowOffsetLabel->setAlignment(Qt::AlignCenter);
    mediumSetLabel->setAlignment(Qt::AlignCenter);
    mediumGetLabel->setAlignment(Qt::AlignCenter);
    mediumOffsetLabel->setAlignment(Qt::AlignCenter);
    highSetLabel->setAlignment(Qt::AlignCenter);
    highGetLabel->setAlignment(Qt::AlignCenter);
    highOffsetLabel->setAlignment(Qt::AlignCenter);

    ui->tableWidget->setCellWidget(0, 1, lowSetLabel);
    ui->tableWidget->setCellWidget(0, 2, lowGetLabel);
    ui->tableWidget->setCellWidget(0, 3, lowOffsetLabel);

    ui->tableWidget->setCellWidget(1, 1, mediumSetLabel);
    ui->tableWidget->setCellWidget(1, 2, mediumGetLabel);
    ui->tableWidget->setCellWidget(1, 3, mediumOffsetLabel);

    ui->tableWidget->setCellWidget(2, 1, highSetLabel);
    ui->tableWidget->setCellWidget(2, 2, highGetLabel);
    ui->tableWidget->setCellWidget(2, 3, highOffsetLabel);

    int rowHeight = 40;
    if (windowWidth >= 1280) {
        rowHeight = 65;
    }

    ui->tableWidget->horizontalHeader()->setFixedHeight(windowWidth >= 1280 ? 85 : 60);
    for (int i = 0;i < 3;i++)
    {
        ui->tableWidget->setRowHeight(i, rowHeight);
        ui->tableWidget->item(i,0)->setTextAlignment(Qt::AlignCenter);
    }
}

void VoltTestResult::retranslateUiSlot()
{
    ui->retranslateUi(this);
}

void VoltTestResult::initWidget()
{
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
    shadow->setColor(QColor(0,0,0,150));
    shadow->setOffset(1, 1);
    shadow->setBlurRadius(16);
    ui->widget->setGraphicsEffect(shadow);
    setAttribute(Qt::WA_TranslucentBackground, true);
    setWindowFlags(windowFlags() | Qt::SubWindow);
    ui->tableWidget->setProperty("noinput",true);//不需要虚拟键盘
    ui->tableWidget->verticalHeader()->hide();
    ui->btnExportVoltRes->hide();

    int rowCount = 3;
    ui->tableWidget->setRowCount(rowCount);
    ui->tableWidget->setItem(0,0,new QTableWidgetItem(tr("低压")));
    ui->tableWidget->setItem(1,0,new QTableWidgetItem(tr("中压")));
    ui->tableWidget->setItem(2,0,new QTableWidgetItem(tr("高压")));
    int columnWidth = 125;
    if (windowWidth >= 1280) {
        columnWidth = 158;
        this->setProperty("cssSize", "large");
        this->setFixedWidth(1280);
        this->setFixedHeight(800);
    }
    for (int i = 0;i < 4;i++)
    {
        ui->tableWidget->setColumnWidth(i, i == 0 ? columnWidth : (columnWidth + 20));
    }
}
